﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Threading;
using System.Threading.Tasks;

namespace Member.Client
{
    public partial class fm_login : Common
    {
        XmlDocument xml = new XmlDocument();
        Model.Users model = new Model.Users();
        Model.Soft soft = new Model.Soft();
        List<Model.Soft> list = new List<Model.Soft>();
        List<string> varsion = new List<string>();
        Task[] tasks = new Task[3];
        static CancellationTokenSource[] tokenSource = new CancellationTokenSource[]{new CancellationTokenSource(),
new CancellationTokenSource(),new CancellationTokenSource()};


        CancellationToken[] token = new CancellationToken[] { tokenSource[0].Token, tokenSource[1].Token, tokenSource[2].Token};


        bool ret;
        string msg;
  
        public fm_login()
        {
            InitializeComponent();
            ListView.CheckForIllegalCrossThreadCalls = false;
        }
        ///载入
        private void fm_login_Load(object sender, EventArgs e)
        {
            //this.Text = "会员登陆";
            //this.Width = 240;
            //this.Height = 194;
            //pLogin.Width = 250; 
            //pLogin.Height = 210; 
            //System.Drawing.Point point = new Point(0, 0);
            //pLogin.Location = point;
            pMain.Show();
        }
        ///关闭事件
        private void fm_login_FormClosing(object sender, FormClosingEventArgs e)
        {
            Close(tbName.Text.Trim());
        }
        ///登陆事件
        private void btnLogin_Click(object sender, EventArgs e)
        {
            xml.LoadXml(Login(tbName.Text, tbPasswd.Text));
            ret = bool.Parse(xml.SelectSingleNode("content/return").InnerText);
            msg = xml.SelectSingleNode("content/msg").InnerText;
            if (ret)
            {
                pLogin.Hide(); 
                pMain.Show();
                //ChangeStatus(tbName.Text.Trim(), 1);
                model = Common.GetModel(xml);
                this.Text = GetLevel(model.Type);
                this.Width = 660; this.Height = 312;
            }
            else { MessageBox.Show(msg); }
        }

        
        
        ///添加任务
        private void btnAddTask_Click(object sender, EventArgs e)
        {
            if (tbCount.Text == null && tbCount.Text == "" && tbTime == null && tbTime.Text == "")
            {
                MessageBox.Show("数量和时间不能为空.");
                return;
            }
            int Count; int Time;
            try
            {
                 Count = int.Parse(tbCount.Text.Trim());
                 Time = int.Parse(tbTime.Text.Trim());
            }
            catch { MessageBox.Show("数量和时间非数字"); return; }
            int i = GetListCount();
            if (tbPackageName.Text.Trim() == "") { MessageBox.Show("包名不能为空"); return; }

            varsion = Net.Brush.GetVarsion();//获取版本号

            soft = Common.GetModel(Net.NetCommon.GetCode(Common.apk_url + tbPackageName.Text), Count, Time);
            ListViewItem first = new ListViewItem(soft.ID.ToString());
            first.SubItems.Add(soft.Name);
            first.SubItems.Add(soft.Download_times.ToString());
            first.SubItems.Add(soft.Corp);
            first.SubItems.Add(soft.Version_name);
            first.SubItems.Add(soft.Count.ToString());
            first.SubItems.Add(soft.Time.ToString());
            first.SubItems.Add("0");
            lvTask.Items.Add(first);
            list.Add(soft);
            try
            {
                tasks[i] = System.Threading.Tasks.Task.Factory.StartNew(() => ThreadStart(i, soft, token[i]), token[i]);
                
            }
            catch (Exception ex) { MessageBox.Show("超过了."); return; }

        }





        /// <summary>
        /// 任务
        /// </summary>
        /// <param name="Items"></param>
        /// <param name="soft"></param>
        /// <param name="ct"></param>
        private void ThreadStart(int Items, Model.Soft soft, CancellationToken ct)
        {
            for (int i = 0; i < soft.Count; i++)
            {
                if (ct.IsCancellationRequested)
                {
                    try { throw new OperationCanceledException(token[int.Parse(i.ToString())]); }
                    catch { }
                    lvTask.Items[Items].SubItems[7].Text = "停止以刷【"+i.ToString()+"】";
                    break;
                }
                else 
                {
                    Net.Brush.Get(soft.Down_url, soft.Apk_md5, soft.ID.ToString(), varsion);
                    lvTask.Items[Items].SubItems[7].Text = i.ToString();
                    Thread.Sleep(DuoJiuShuaWan(soft.Time, soft.Count));
                }
            }
        }

        /// <summary>
        /// 获取ListView总数
        /// </summary>
        /// <returns></returns>
        int GetListCount()
        {
            return lvTask.Items.Count;
        }
        /// <summary>
        /// 多久刷完
        /// </summary>
        /// <param name="ShiJian">时间</param>
        /// <param name="Liang">数量</param>
        /// <returns>返回延迟</returns>
        int DuoJiuShuaWan(int ShiJian, int Liang)
        {
            //                  分   秒   毫秒
            ShiJian = ShiJian * 60 * 60 * 1000;
            Liang = ShiJian / Liang;
            return Liang;//等于所需时间毫秒
        }


    }
}
